#include "VAmp.h"


VAmp::VAmp(void)
{
#ifdef HAVE_VAMP_LIBRARY
	Instrument::Instrument();
	FastMode = false;
	int i;
	for (i=0; i<16; i++)
		ChFlag[i]=true;
	for (i=0; i<2; i++)
		AuxFlag[i]=true;
	FirstAmp = new VAmpLib();
	MyName = _("VAmp - BrainAmp");
#else
		wxMessageBox(_("This build doesn't support BrainProducts VAmp USB"));
#endif
}

VAmp::~VAmp(void)
{
#ifdef HAVE_VAMP_LIBRARY
	if (FirstAmp != NULL)
	{
		if (IsRunning)
		{
			FirstAmp->VAmpStop();
			IsRunning = false;
		}
		FirstAmp->VAmpClose();
		delete FirstAmp;
		FirstAmp = NULL;
	}
#endif
}

#ifdef HAVE_VAMP_LIBRARY
void* VAmp::Entry(void)
{
	BOOL ret;
	int Index = 0;
	
	int i;
	int inc;
	int PointsPerBlock;
	
	SampleStruct	Signal;
	memset(&Signal,0,sizeof(Signal));
	Signal.Data.DataLen	=	Channels;
	
	if(FirstAmp!=NULL)
	{
		if (FirstAmp->DeviceProp.RateEeg*0.001*DEVICE_GET_DATA_INTERVAL>1)
			PointsPerBlock = FirstAmp->DeviceProp.RateEeg*0.001*DEVICE_GET_DATA_INTERVAL;
	}

	Esci = false;

	while(!Esci)
	{
		if(IsRunning)
		{
			//SetDisplayImg(_T(".//Media//"),_T("VAmpRec.bmp"));
			FirstAmp->GetData();
			inc = floor(PointsPerBlock/floor(DEVICE_GET_DATA_INTERVAL*0.001*frequenza));
			Index = 0;
			ret =0;
			for (int j=0; j<PointsPerBlock; j+=inc)
			{
				if (FastMode == false)
				{
					switch (FirstAmp->DeviceInfo.Model)
					{
					case FA_MODEL_8:
						for (i=0; i<FirstAmp->EEGChannels; i++)
						{
							if (ChFlag[i])
								Signal.Data.Signal[i] = (float)((float)FirstAmp->Buffer8[j].Main[i]-(float)FirstAmp->Buffer8[j].Main[FirstAmp->EEGChannels])*FirstAmp->DeviceProp.ResolutionEeg*1e6;
						}
						if (AuxFlag[0])
							Signal.Data.Signal[FirstAmp->EEGChannels] = FirstAmp->Buffer8[j].Aux[0]*FirstAmp->DeviceProp.ResolutionAux;
						break;
					case FA_MODEL_16:
						for (i=0; i<FirstAmp->EEGChannels; i++)
						{
							if (ChFlag[i])
								Signal.Data.Signal[i] = (float)((float)FirstAmp->Buffer16[j].Main[i]-(float)FirstAmp->Buffer16[j].Main[FirstAmp->EEGChannels])*FirstAmp->DeviceProp.ResolutionEeg*1e6;
						}
						if (AuxFlag[0])
							Signal.Data.Signal[FirstAmp->EEGChannels] = (float)(FirstAmp->Buffer16[j].Aux[0]*FirstAmp->DeviceProp.ResolutionAux*1e6);
						if (AuxFlag[1])
							Signal.Data.Signal[FirstAmp->EEGChannels+1] = (float)(FirstAmp->Buffer16[j].Aux[1]*FirstAmp->DeviceProp.ResolutionAux*1e6);
						break;
					}
				}
				else
					for (i=0; i<FA_MODE_20_KHZ_CHANNELS_MAIN; i++)
					{
						if (ChFlag[i])
							Signal.Data.Signal[i] = (float)FirstAmp->Buffer20k[j].Main[i];
					}
				PutSample(&Signal);
			}
		}
		else
		{
			//SetDisplayImg(_T(".//Media//"),_T("VAmpImg.bmp"));
			Sleep(100);		
		}
	}
	if (FirstAmp!=NULL)
	{
		FirstAmp->VAmpStop();
		SetDisplayImg(_T(".//Media//"),_T("VAmpImg.bmp"));
		FirstAmp->VAmpClose();
	}
	return NULL;
}
void VAmp::StartDevice(wxString FileName,wxString FilePath,bool File)
{
	Instrument::StartDevice(FileName,FilePath,File);
	if (FirstAmp!=NULL)
	{
		FirstAmp->VAmpStart();
		SetDisplayImg(_T(".//Media//"),_T("VAmpRec.bmp"));
	}
	IsRunning = true;
	
}

void VAmp::StopDevice(void)
{
	IsRunning = false;
	Sleep (500);
	Esci = true;
	Instrument::StopDevice();
}
int VAmp::OpenPort(const char *PortName, const char *OpenVal2, const char *OpenVal3)
{
	
	//Control if there is the device
	if (FirstAmp->CheckDevice()!=0)
		return -1;

	//GetInfo
	if (FirstAmp->GetInfo()!=0)
		return -1;

	int ContCh =0;
	//Show dialog for Frequency and channels selections
	VAmpDiag VampDlg(NULL,FirstAmp->EEGChannels, &ChFlag[0], &AuxFlag[0], &FastMode, &Labels[0], &ContCh);
//	VAmpDiag VampDlg(NULL,16, &ChFlag[0], &AuxFlag[0], &FastMode, &Labels[0], &ContCh);
	VampDlg.ShowModal();
	
	//Connect to the device
	if (FirstAmp->VAmpConnect(FastMode == true ? dm20kHz4Channels : dmNormal)!=FA_ERR_OK)
		return -1;
	
	//Set Init image on the device's screen
	SetDisplayImg(_T(".//Media//"),_T("VAmpInit.bmp"));
	
	//Set the memory usage depending on start mode
	FirstAmp->SetData(IAC_AT_DATA,FastMode);
	
	//VAmpDevice->EEGChannels + VAmpDevice->AUXChannels;
	//
	int nCh = 0;
	for (int i=0; i<FirstAmp->EEGChannels; i++)
	{
		Maximum[i] = (int)((float)(FirstAmp->DeviceProp.RangeEeg)/2*1e3);				//in mV
		Minimum[i] = (int)((float)(-FirstAmp->DeviceProp.RangeEeg)/2*1e3);
		if (ChFlag[i])
		{
			if (Labels[nCh].empty())
				Labels[nCh] = wxString::Format(wxT("EEG%d"),i+1);
			nCh++;
		}
	}
	for (int i=FirstAmp->EEGChannels; i<(FirstAmp->EEGChannels+FirstAmp->AUXChannels); i++)
	{
		Maximum[i] = (int)((float)(FirstAmp->DeviceProp.RangeAux)/2*1e3);				//in mV
		Minimum[i] = -Maximum[i];
		if (AuxFlag[i-FirstAmp->EEGChannels])
		{
			if (Labels[nCh].empty())
				Labels[nCh] = wxString::Format(wxT("AUX%d"),i-FirstAmp->EEGChannels+1);
			nCh++;
		}
	}
	yLabel = L"mV";

	Resolution = FirstAmp->DeviceProp.ResolutionEeg*1e6;
	DigitalOffset = 0;
	DigitalMax	=	((FirstAmp->DeviceProp.RangeEeg)/2.0f*1e3)/Resolution;
	DigitalMin	=	-DigitalMax;

	if (FastMode)
		frequenza = 20000;	//High freq
	else
		frequenza = 2000;   //Low freq
	
	Initialize(ContCh,frequenza,20);	//Setto il numero di canali in base a quelli selezionati
	
	SetDisplayImg(_T(".//Media//"),_T("VAmpImg.bmp"));
	return 0;

}


int VAmp::SetDisplayImg(wxString FilePath, wxString FileName)
{
	if (FirstAmp!=NULL)
	{
		HBITMAP Bitmap;
		wxString BitMapFile;
		//CARICARE IMMAGINE
		BitMapFile= FilePath+FileName;
		Bitmap = (HBITMAP)LoadImage(NULL, BitMapFile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
		if (FA_ERR_OK != faSetBitmap(FirstAmp->DeviceId,Bitmap))
			return -1;
		faSetContrast(FirstAmp->DeviceId,100);
		faSetBrightness(FirstAmp->DeviceId,100);
		return 1;
	}
	else 
		return -1;
}
#endif